package cn.livingCloud.controller.core;

import cn.livingCloud.dto.AccountChangePasswordDto;
import cn.livingCloud.dto.EmployeeAccountDto;
import cn.livingCloud.dto.EmployeeAccountFormDto;
import cn.livingCloud.dto.EmployeeAccountListDto;
import cn.livingCloud.service.EmployeeAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

/**
 * 员工账号管理
 * Created by 唐旭 on 2016/5/30.
 */
@Controller
@RequestMapping("/admin/organization/account")
public class EmployeeAccountController {

    @Autowired
    private EmployeeAccountService service;

    //列表
    @RequestMapping("/list.htm")
    public String loadList(EmployeeAccountListDto listDto, Model model) {
        service.loadList(listDto);
        model.addAttribute("listDto", listDto);
        return "organization/account/list";
    }

    //更改状态
    @RequestMapping("/change_state")
    public String changeState(String uuid, boolean state, Model model) {
        EmployeeAccountDto userAccountDto = service.updateState(uuid, state);
        model.addAttribute("accountDto", userAccountDto);
        return "organization/account/employee_account_list_item";
    }

    //进入更改密码界面
    @RequestMapping("/change_password_from.htm")
    public String loadChangePassword(String uuid, Model model) {
        AccountChangePasswordDto userAccountDto = service.findUserAccountDtoByUuid(uuid);
        model.addAttribute("accountDto", userAccountDto);
        return "organization/account/change_password_from";
    }

    //更改密码
    @RequestMapping(value = "/change_password_from.htm", method = RequestMethod.POST)
    public String changePassword(@ModelAttribute("accountDto") @Valid AccountChangePasswordDto accountDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "organization/account/change_password_from";
        }
        service.updatePassword(accountDto);
        model.addAttribute("change_password", "success");
        return "redirect:list.htm";
    }

    //编辑/添加
    @RequestMapping("/edit.htm")
    public String edit(String uuid, String employeeUuid, Model model) {
        EmployeeAccountFormDto formDto = service.loadEmployeeAccountForm(uuid, employeeUuid);
        model.addAttribute("formDto", formDto);
        return "organization/account/edit_account";
    }

    //提交编辑信息
    @RequestMapping(value = "/edit.htm", method = RequestMethod.POST)
    public String editUser(@ModelAttribute("formDto") @Valid EmployeeAccountFormDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "organization/account/edit_account";
        }
        service.updateEmployeeAccount(formDto);
        model.addAttribute("edit", "success");
        if (formDto.isContinuation()) {
            return "redirect:edit.htm?edit=success";
        }
        model.addAttribute("employeeUuid", formDto.getEmployeeUuid());
        return "redirect:list.htm";
    }

    //删除
    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    public String deleteUser(String uuid, Model model) {
        service.deleteAccount(uuid);
        model.addAttribute("delete", "success");
        return "redirect:list.htm";
    }


}
